<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2012.03.17.
   家居商家模板控制器($小牛New $2014.07.28)*/

!defined('Q_PATH') && exit;

class JiajucompanytemplateController extends AController{

	public function init__(){
		$this->_arrNotMethod=array(
			'save_previewstatus',
			'save_preview',
			'view_preview',
			'foreverdelete',
			'foreverdelete_deep',
			'input_change_ajax',
			'input_change_unique',
			'forbid',
			'closeitem',
			'openitem',
			'resume',
			'clear_recycle',
			'save_sort',
			'insert',
			'add',
			'edit',
			'update'
		);
		
		parent::init__();
	}
	
	public function index($sName=null,$bDisplay=true){
		// 模板套系
		$arrTemplate=Jiaju_Extend::getThemePreview();

		$arrCompanyVip=Model::F_('jiajucompanyvip')->where(array('jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id']))->getOne();
		if(!$arrCompanyVip['jiajucompanyvip_template']){
			$arrCompanyVip['jiajucompanyvip_template']='default';
		}

		// 当前模板
		$arrCurrentTemplates=Model::F_('jiajucompanybanner')->where(array('jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id']))->getAll();
	
		$this->assign('arrTemplate',isset($arrTemplate[$arrCompanyVip['jiajucompanyvip_template']])?$arrTemplate[$arrCompanyVip['jiajucompanyvip_template']]:$arrTemplate['default']);
		$this->assign('arrCurrentTemplates',$arrCurrentTemplates);
		$this->display();
	}

	public function template(){
		if($GLOBALS['_company_']['jiajucompany_isfvip']==0){
			$this->E('只有VIP才能够设置');
		}
		
		// 当前模板
		$arrCurrentTemplates=Model::F_('jiajucompanybanner')->where(array('jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id']))->getAll();

		$arrTemplate=Jiaju_Extend::getThemePreview();

		$oCompanyVip=JiajucompanyvipModel::F()->where(array('jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id']))->getOne();
		if(!$oCompanyVip['jiajucompanyvip_template']){
			$oCompanyVip['jiajucompanyvip_template']='default';
		}
	
		if($this->isPost()){
			$oCompanyVip->save('update');
			if($oCompanyVip->isError()){
				$this->E($oCompanyVip->getErrorMessage());
			}else{
				// 清理广告banner设置
				Model::M_('jiajucompanybanner')->deleteWhere(array('jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id']));

				if(!empty($_POST['jiajucompanybanner']['img'])){
					foreach($_POST['jiajucompanybanner']['img'] as $nKey=>$sValue){
						if($sValue){
							Model::M_('jiajucompanybanner')->insertWhere(
								array(
									'jiajucompany_id'=>$GLOBALS['_company_']['jiajucompany_id'],
									'jiajucompanybanner_filename'=>$sValue,
									'jiajucompanybanner_filetitle'=>!empty($_POST['jiajucompanybanner']['title'][$nKey])?$_POST['jiajucompanybanner']['title'][$nKey]:'',
									'jiajucompanybanner_link'=>!empty($_POST['jiajucompanybanner']['link'][$nKey])?$_POST['jiajucompanybanner']['link'][$nKey]:'',
									'create_dateline'=>CURRENT_TIMESTAMP
								)
							);
						}
					}
				}
				
				$this->S('保存商家VIP模板设置成功');
			}
		}
		
		$this->assign('arrTemplate',$arrTemplate);
		$this->assign('arrCurrentTemplates',$arrCurrentTemplates);
		$this->assign('sCurrentTemplate',$oCompanyVip['jiajucompanyvip_template']);
		$this->display();
	}

}
